package com.zhou.common.mybatis.template.shiro;

import org.flowable.engine.ProcessEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class FlowableTest3Application  implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(FlowableTest3Application.class, args);
    }


    @Autowired
    private ProcessEngine processEngine;

/*    @Override
    public void run(String... args) throws Exception {
        TaskService taskService = processEngine.getTaskService();
        FormService formService = processEngine.getFormService();
        HistoryService historyService = processEngine.getHistoryService();

        RepositoryService repositoryService = processEngine.getRepositoryService();
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().active().list();
        list.forEach(System.out::println);

        ProcessDefinition test1 = repositoryService.createProcessDefinitionQuery().processDefinitionKey("test1").singleResult();

        RuntimeService runtimeService = processEngine.getRuntimeService();


        List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("test_group").list();
        tasks.forEach(System.out::println);
        tasks = taskService.createTaskQuery().taskCandidateUser("test").list();
        tasks.forEach(System.out::println);
        tasks = taskService.createTaskQuery().taskAssignee("xianfeng").list();
        tasks.forEach(System.out::println);

        Map<String, Object> variables = new HashMap<String, Object>();
        //设置审批人
        variables.put("test1", "xianfeng");
        *//*ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey("test1", variables);

        System.out.println(processInstance);*//*


        List<HistoricProcessInstance> list1 = historyService.createHistoricProcessInstanceQuery().list();
        System.out.println(list1);


        tasks = taskService.createTaskQuery().taskCandidateGroup("test_group").list();
        tasks.forEach(System.out::println);
        tasks = taskService.createTaskQuery().taskCandidateUser("test").list();
        tasks.forEach(System.out::println);
        tasks = taskService.createTaskQuery().taskAssignee("xianfeng").list();
        tasks.forEach(System.out::println);
        String taskFormKey = formService.getTaskFormKey(tasks.get(0).getProcessDefinitionId(), tasks.get(0).getTaskDefinitionKey());
        TaskFormData taskFormData = formService.getTaskFormData(tasks.get(0).getId());
        System.out.println(taskFormKey);
        System.out.println(taskFormData);
        FormInfo taskFormModel = taskService.getTaskFormModel(tasks.get(0).getId());
        System.out.println(taskFormModel);
        Task task=tasks.get(0);
        taskService.completeTaskWithForm(task.getId(),taskFormModel.getId(),"outcome", Maps.newHashMap());
    }*/

    @Override
    public void run(String... args) throws Exception {
        /*TaskService taskService = processEngine.getTaskService();
        FormService formService = processEngine.getFormService();
        HistoryService historyService = processEngine.getHistoryService();

        RepositoryService repositoryService = processEngine.getRepositoryService();
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().active().list();
        list.forEach(System.out::println);

        List<ProcessDefinition> test1 = repositoryService.createProcessDefinitionQuery().processDefinitionKey("test1").latestVersion().list();

        RuntimeService runtimeService = processEngine.getRuntimeService();

        List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("test1_group1").list();
        tasks.forEach(System.out::println);
        tasks = taskService.createTaskQuery().taskCandidateUser("test11").list();
        tasks.forEach(System.out::println);
        tasks = taskService.createTaskQuery().taskAssignee("xianfeng").list();
        tasks.forEach(System.out::println);

        Map<String, Object> variables = new HashMap<String, Object>();
        //设置审批人
        variables.put("test1", "test11");
        variables.put("test1_group", "test1_group1");
        *//*ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey("test2", variables);

        System.out.println(processInstance);*//*


        List<HistoricProcessInstance> list1 = historyService.createHistoricProcessInstanceQuery().list();
        System.out.println(list1);


        tasks = taskService.createTaskQuery().taskCandidateGroup("test1_group1").list();
        tasks.forEach(System.out::println);
        tasks = taskService.createTaskQuery().taskCandidateUser("test11").list();
        tasks.forEach(System.out::println);
        //领取任务
//        Task task=tasks.get(0);
//        taskService.claim(task.getId(),"test11");
        tasks = taskService.createTaskQuery().taskAssignee("test11").list();
        tasks.forEach(System.out::println);
        Task task=tasks.get(0);
        //完成任务
        variables = new HashMap<String, Object>();
        //设置审批人
        variables.put("test2", "test22");
        variables.put("test2_group", "test2_group2");
        taskService.complete(task.getId(),variables);
        tasks = taskService.createTaskQuery().taskCandidateGroup("test2_group2").list();
        tasks.forEach(System.out::println);
        tasks = taskService.createTaskQuery().taskCandidateUser("test22").list();
        tasks.forEach(System.out::println);

        String taskFormKey = formService.getTaskFormKey(tasks.get(0).getProcessDefinitionId(), tasks.get(0).getTaskDefinitionKey());
        TaskFormData taskFormData = formService.getTaskFormData(tasks.get(0).getId());
        System.out.println(taskFormKey);
        System.out.println(taskFormData);
        FormInfo taskFormModel = taskService.getTaskFormModel(tasks.get(0).getId());
        System.out.println(taskFormModel);
        task=tasks.get(0);
        taskService.completeTaskWithForm(task.getId(),taskFormModel.getId(),"outcome", Maps.newHashMap());*/
    }
}

